﻿#region
/*******************************************************************************
** File Name:		    AdminUserRepository.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using Phoenix.Entity;
using Phoenix.Respository;
using IBatisNet.DataMapper;
using Phoenix.Data.IBatis;
using System.Collections;

namespace Phoenix.IBatisRepository
{
    public class AdminUserRepository : BaseIBatisDao,IAdminUser 
    {
    
		#region  Auto Generation

        public AdminUserRepository()
            : base("/Config/IBatis/86TCMap.Config")
        {

        }
        		
        public void AddAdminUser(EAdminUser entity)
        {
			string stmtId = "AdminUserMap.InsertAdminUser";
			base.SqlMapper.Insert(stmtId, entity);
        }
        
		public  EAdminUser GetByUserID (System.Int32 userID)
        {
			string stmtId = "AdminUserMap.SelectByUserID";
			return base.SqlMapper.QueryForObject<EAdminUser>(stmtId, userID);
        }
        
        public  void DeleteByUserID (System.Int32 userID)
        {
			string stmtId = "AdminUserMap.DeleteByUserID";
			base.SqlMapper.Update(stmtId, userID);
        }
        
        public  void UpdateByUserID (EAdminUser entity)
        {
			string stmtId = "AdminUserMap.UpdateByUserID";
			base.SqlMapper.Update(stmtId, entity);
        }


		#endregion

        public IList<EAdminUser> GetPagedList(int pageSize, int pageIndex, string trueName, string userName, DateTime? beginDate, DateTime? endDate, out int totalRecord)
        {
            Hashtable ht = new Hashtable();
            ht.Add("UserName", userName);
            ht.Add("TrueName", trueName);
            ht.Add("BeginDate", beginDate);
            ht.Add("EndDate", endDate);


            totalRecord = base.SqlMapper.QueryForObject<int>("AdminUserMap.SelectRecord", ht);

            if (pageIndex > totalRecord)
                pageIndex = 1;

            int startPage = pageSize * pageIndex - pageSize;

            int endPage = pageSize * pageIndex;

            ht.Add("Offset", startPage);
            ht.Add("Rows", endPage);

            //offset,rows
            string stmtId = "AdminUserMap.SelectPagedList";

            return base.SqlMapper.QueryForList<EAdminUser>(stmtId, ht);


        }

        public void UpdateStatus(int userID, bool status)
        {
            Hashtable ht = new Hashtable();
            ht.Add("UserID", userID);
            ht.Add("Status", status);
            string stmtId = "AdminUserMap.UpdateStatus";
            base.SqlMapper.QueryForList<EAdminUser>(stmtId, ht);
        }

        public EAdminUser GetByUserName(string userName)
        {
            string stmtId = "AdminUserMap.SelectByUserName";
            return base.SqlMapper.QueryForObject<EAdminUser>(stmtId, userName);
        }

        public bool GetExistsUserName(string userName)
        {
            string stmtId = "AdminUserMap.SelectExistsUserName";

            return base.SqlMapper.QueryForObject<bool>(stmtId, userName);
            
        }


        /// <summary>
        /// 批量修改部门级别
        /// </summary>
        /// <param name="newLevel">新部门级别</param>
        /// <param name="oldLevel">旧部门级别</param>
        public void BatchUpdateByDepartLevel(string newLevel, string oldLevel)
        {
            //string hsql = " Update EAccount  SET LEVEL =REPLACE(LEVEL,'," + oldLevel + "','," + newLevel + "') ";
            //HQuery hq = new HQuery(typeof(EAccount), hsql);

            //using (IRepository repository = SessionFactoryFactory.Default.CreateRepository())
            //{
            //    repository.Execute(hq);
            //}
            //removeCache();
        }
    }
}

